package com.xmut.mall.order.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmut.mall.order.annotation.CheckJwt;
import com.xmut.mall.order.utils.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xmut.mall.order.entity.OrderItemEntity;
import com.xmut.mall.order.service.OrderItemService;
import com.xmut.mall.order.utils.PageUtils;
import com.xmut.mall.order.utils.R;

@RestController
@RequestMapping("order/orderitem")
public class OrderItemController {
    @Autowired
    private OrderItemService orderItemService;

    @CheckJwt
    @PutMapping("/updateOrderItem")
    public RespBean updateOrderItem(@RequestBody OrderItemEntity entity){
        boolean b = orderItemService.updateById(entity);
        if (b){
            return RespBean.success("修改成功");
        }else {
            return RespBean.error("修改失败");
        }
    }

    @CheckJwt
    @GetMapping("/getOrderItemById/{id}")
    public RespBean getOrderItemById(@PathVariable Long id) {
        OrderItemEntity orderItem = orderItemService.getById(id);
        if (orderItem == null) {
            return RespBean.error("订单项不存在");
        } else {
            return RespBean.success("获取成功", orderItem);
        }
    }

    @CheckJwt
    @GetMapping("/getOrderItemBySn/{sn}")
    public RespBean getOrderItemBySn(@PathVariable String sn) {
        OrderItemEntity item = orderItemService.getOne(new QueryWrapper<OrderItemEntity>().eq("sn", sn));
        if (item == null) {
            return RespBean.error("订单项不存在");
        } else {
            return RespBean.success("获取成功", item);
        }
    }

    /**
     * 创建订单项
     *
     * @param entity
     * @return
     */
    @CheckJwt
    @PostMapping("/createOrderItem")
    public RespBean createOrderItem(@RequestBody List<OrderItemEntity> entity) {
        return orderItemService.createOrderItem(entity);
    }
}
